package com.bawei.songyingyu.model.util

import com.wd.live.module_base.net.ApiService

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit

/**
 * Created by Android Studio.
 * author: songyingyu
 * Date: 2024/5/16
 * Time: 15:03
 * 功能：网络工具类   //7)封装网络请求（Retrofit+OkHttp+RxJava+双重校验锁单例）
 */
class HttpUtils private constructor() {
    //8)提高软件性能，防止增加手机耗电量，避免频繁地创建与销毁常用对象，使用设计模式（双重校验锁单例）
//9)保证线程安全，避免线程隐患
    companion object {
        val httpUtils: HttpUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            HttpUtils()
        }
    }
    private val retrofit=Retrofit.Builder()
        .baseUrl("http://10.59.9.24/")
        .client(OkHttpClient.Builder()
            .connectTimeout(5,TimeUnit.SECONDS)
            .readTimeout(5,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().apply {
                level=HttpLoggingInterceptor.Level.BODY
            })
            .build())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()
    val apiService=retrofit.create<ApiService>()
}